package com.clms.maintain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.maintain.mapper.RecordMapper;
import com.clms.maintain.pojo.entity.TbRecord;
import com.clms.maintain.pojo.vo.RecordVo;
import com.clms.maintain.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-third
 * @description: 维修记录表的实现类
 * @author: 许鹏
 * @create: 2021-07-23 14:40
 **/
@Service
@ApiModel(value = "维修实现",description ="维修厂的 逻辑层" )
public class RecordServiceImpl implements RecordService {

    @Resource
    private RecordMapper recordMapper;



    @Override
    @ApiModelProperty(value = "查询所有的维修记录")
    public R selectRecordAll(Integer currentPage, Integer pageSize,RecordVo recordVo) {

        Page<TbRecord> page = new Page(currentPage,pageSize);
        QueryWrapper<TbRecord> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(recordVo.getName())){
            wrapper.like("name",recordVo.getName());
        }

        if (StringUtils.isNotEmpty(recordVo.getCarName())){
            wrapper.like("car_Name",recordVo.getCarName());
        }
        if (StringUtils.isNotEmpty(recordVo.getAmount())){
            wrapper.like("amount",recordVo.getAmount());
        }

        IPage<TbRecord> data = recordMapper.selectRecordAll(page,wrapper);
        return R.ok().put("total",data.getTotal()).put("records",data.getRecords());
    }

    @Override
    @ApiModelProperty(value = "添加一条维修记录")
    public R insertRecord(TbRecord tbRecord) {
        int insert = recordMapper.insert(tbRecord);
        if (insert>0){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");
    }

    @Override
    @ApiModelProperty(value = "修改维修记录")
    public R updateRecord(TbRecord tbRecord) {
        int i = recordMapper.updateById(tbRecord);
        if (i>0){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");

    }

    @Override
    @ApiModelProperty(value="根据id查询维修记录")
    public R selectById(Integer id) {
        TbRecord tbRecord = recordMapper.selectById(id);
        return R.ok().put("tbRecord",tbRecord);
    }

    @Override
    @ApiModelProperty(value = "删除维修记录")
    public R deleteRecord(Integer id) {
        int i = recordMapper.deleteById(id);
        if (i>0){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }
}
